package com.example.vaccine.controller;

import com.example.vaccine.VO.ResultVO;
import com.example.vaccine.entity.Menu;
import com.example.vaccine.util.ResultVOUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
@Tag(name = "菜单接口", description = "根据用户角色获取对应菜单")
public class MenuController {

    // 模拟数据，实际请用数据库查询
    private static final Map<String, List<Menu>> roleMenuMap = new HashMap<>();

    static {
        // admin角色菜单
        List<Menu> adminMenus = new ArrayList<>();
        // 首页
        adminMenus.add(new Menu("Dashboard", "/dashboard", "首页", "HomeFilled", true, "Dashboard", null));

        // 系统设置
        List<Menu> systemSettingsChildren = new ArrayList<>();
        systemSettingsChildren.add(new Menu("VaccinesManagement", "vaccines-management", "疫苗信息", "Menu", false, "VaccinesManagement", null));
        systemSettingsChildren.add(new Menu("CheckManagement", "check-management", "核查管理", "User", false, "CheckManagement", null));
        systemSettingsChildren.add(new Menu("EmployeeManagement", "employee-management", "权限管理", "UserFilled", false, "EmployeeManagement", null));
        adminMenus.add(new Menu("systemSetting", "/system-settings", "系统设置", "Setting", true, null, systemSettingsChildren));

        // 核查列表
        List<Menu> checkListChildren = new ArrayList<>();
        checkListChildren.add(new Menu("CheckRecord", "check-record", "核查记录", "List", false, "CheckRecord", null));
        checkListChildren.add(new Menu("CheckTask", "check-task", "核查任务", "Histogram", false, "CheckTask", null));
        adminMenus.add(new Menu("checkList", "/checkList", "核查列表", "Monitor", true, null, checkListChildren));

        // 个人中心
        List<Menu> profileChildren = new ArrayList<>();
        profileChildren.add(new Menu("ModifyPwd", "modifyPwd", "修改密码", "PieChart", false, "ModifyPwd", null));
        profileChildren.add(new Menu("Info", "info", "个人信息", "Histogram", false, "Info", null));
        adminMenus.add(new Menu("profile", "/profile", "个人中心", null, false, null, profileChildren));

        // 反馈中心
        List<Menu> messageCenterChildren = new ArrayList<>();
        messageCenterChildren.add(new Menu("doctorMsg", "doctor", "反馈记录", "List", false, "Doctor", null));
        messageCenterChildren.add(new Menu("infoSetting", "infoSetting", "通知设置", "Picture", false, "InfoSetting", null));
        adminMenus.add(new Menu("messageCenter", "/messageCenter", "反馈中心", "Message", true, null, messageCenterChildren));

        // 添加到角色路由 Map 中
        roleMenuMap.put("admin", adminMenus);

        // checker角色菜单
        List<Menu> checkerMenus = new ArrayList<>();
        checkerMenus.add(new Menu("Dashboard", "/dashboard", "首页", "HomeFilled", true, "Dashboard", null));

        List<Menu> checkListChildren1 = new ArrayList<>();
        checkListChildren1.add(new Menu("CheckRecord", "check-record", "核查记录", "List", false, "CheckRecord", null));
        checkListChildren1.add(new Menu("CheckTask", "check-task", "核查任务", "Histogram", false, "CheckTask", null));
        checkerMenus.add(new Menu("checkList", "/checkList", "核查列表", "Monitor", true, null, checkListChildren1));

        // 操作页面
        List<Menu> operationsChildren = new ArrayList<>();
        operationsChildren.add(new Menu("Checker", "checker", "核查人员", "Picture", false, "Checker", null));
        checkerMenus.add(new Menu("Operations", "/operations", "操作页面", "Odometer", true, null, operationsChildren));

        // 个人中心
        List<Menu> profileChildren3 = new ArrayList<>();
        profileChildren3.add(new Menu("ModifyPwd", "modifyPwd", "修改密码", "PieChart", false, "ModifyPwd", null));
        profileChildren3.add(new Menu("Info", "info", "个人信息", "Histogram", false, "Info", null));
        checkerMenus.add(new Menu("profile", "/profile", "个人中心", null, false, null, profileChildren3));

        roleMenuMap.put("checker", checkerMenus);

        // doctor角色菜单
        List<Menu> doctorMenus = new ArrayList<>();

        // 首页
        doctorMenus.add(new Menu("Dashboard", "/dashboard", "首页", "HomeFilled", true, "Dashboard", null));

        // 操作页面（医生界面）
        List<Menu> operationsChildren3 = new ArrayList<>();
        operationsChildren3.add(new Menu("Doctor", "doctor", "领取界面", "List", false, "Doctor", null));
        operationsChildren3.add(new Menu("GetedVaccines", "GetedVaccines", "已取疫苗", "List", false, "GetedVaccines", null));
        operationsChildren3.add(new Menu("VaccineFeedback", "VaccineFeedback", "疫苗反馈", "List", false, "VaccineFeedback", null));
        doctorMenus.add(new Menu("Operations", "/operations", "操作页面", "Odometer", true, null, operationsChildren3));

        // 反馈中心（医生主要使用的模块）
        List<Menu> messageCenterChildren3 = new ArrayList<>();
        messageCenterChildren3.add(new Menu("doctorMsg", "doctor", "反馈记录", "List", false, "Doctor", null));
        doctorMenus.add(new Menu("messageCenter", "/messageCenter", "反馈中心", "Message", true, null, messageCenterChildren3));

        // 个人中心
        List<Menu> profileChildren2 = new ArrayList<>();
        profileChildren2.add(new Menu("ModifyPwd", "modifyPwd", "修改密码", "PieChart", false, "ModifyPwd", null));
        profileChildren2.add(new Menu("Info", "info", "个人信息", "Histogram", false, "Info", null));
        doctorMenus.add(new Menu("profile", "/profile", "个人中心", null, false, null, profileChildren2));

        // 添加到角色路由 Map 中
        roleMenuMap.put("doctor", doctorMenus);
    }

    @GetMapping("/menus")
    @Operation(summary = "根据角色获取菜单列表", description = "传入角色名称，返回该角色的菜单数据")
    public ResultVO getMenus(
            @Parameter(name = "role", description = "用户角色名，比如admin, checker, doctor", required = true, in = ParameterIn.QUERY)
            @RequestParam String role) {
        List<Menu> menus = roleMenuMap.get(role);
        if (menus == null) {
            menus = new ArrayList<>();  // 避免返回null
        }
        return ResultVOUtil.success(menus);
    }

}
